#encoding: utf-8 
require 'rubygems'
require 'nokogiri'
require 'open-uri'

SERVER = 'http://all.wasu.cn'

namespace :db do
  namespace :movies do
    task :load => :environment do
      load_movies
    end

    task :reset => :environment do
      Video.delete_all :kind => 'movie'
      Category.delete_all :kind => 'movie'
    end
    
    def load_movies
      puts "load movies......"
      index = "http://all.wasu.cn/index/cid/1"
      {
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=1" => "情感生活",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=2" => "情感生活", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=3" => "情感生活", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=4" => "情感生活",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=5" => "情感生活",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=6" => "情感生活", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=7" => "情感生活",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=8" => "情感生活",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=9" => "情感生活", 
   
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=10" => "言情偶像",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=11" => "言情偶像",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=12" => "言情偶像", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=13" => "言情偶像",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=14" => "言情偶像",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=15" => "言情偶像", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=16" => "言情偶像",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=17" => "言情偶像",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=18" => "言情偶像", 

        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=19" => "怀旧剧情",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=20" => "怀旧剧情",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=21" => "怀旧剧情", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=22" => "怀旧剧情",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=23" => "怀旧剧情",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=24" => "怀旧剧情", 
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=25" => "怀旧剧情",  
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=26" => "怀旧剧情",
        "http://all.wasu.cn/index/sort/time/cid/6/class/program?&p=27" => "怀旧剧情", 

        "http://all.wasu.cn/index/sort/time/cid/5/class/program?p=1" => "战争史诗",
        "http://all.wasu.cn/index/sort/time/cid/5/class/program?p=2" => "战争史诗",
        "http://all.wasu.cn/index/sort/time/cid/5/class/program?p=3" => "战争史诗",
        "http://all.wasu.cn/index/sort/time/cid/5/class/program?p=4" => "战争史诗",
        
        
        "http://all.wasu.cn/index/sort/time/cid/3/class/program?p=1" => "惊辣悬疑",
        "http://all.wasu.cn/index/sort/time/cid/3/class/program?p=2" => "惊辣悬疑",
        "http://all.wasu.cn/index/sort/time/cid/3/class/program?p=3" => "惊辣悬疑",
        
        
        "http://all.wasu.cn/index/sort/time/cid/8/class/program?&p=1" => "喜剧幽默",
        "http://all.wasu.cn/index/sort/time/cid/8/class/program?&p=2" => "喜剧幽默",
        "http://all.wasu.cn/index/sort/time/cid/8/class/program?&p=3" => "喜剧幽默",
        "http://all.wasu.cn/index/sort/time/cid/8/class/program?&p=4" => "喜剧幽默",
        "http://all.wasu.cn/index/sort/time/cid/8/class/program?&p=5" => "喜剧幽默",
        "http://all.wasu.cn/index/sort/time/cid/8/class/program?&p=6" => "喜剧幽默",
        
        "http://all.wasu.cn/index/sort/time/cid/10/class/program?&p=1" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/10/class/program?&p=2" => "卡通动漫",
        "http://all.wasu.cn/index/sort/time/cid/10/class/program?&p=3" => "卡通动漫",
        
        "http://all.wasu.cn/index/sort/time/cid/7/class/program?&p=1" => "科幻灾难",
        "http://all.wasu.cn/index/sort/time/cid/7/class/program?&p=2" => "科幻灾难",
        "http://all.wasu.cn/index/sort/time/cid/7/class/program?&p=3" => "科幻灾难",
        "http://all.wasu.cn/index/sort/time/cid/7/class/program?&p=4" => "科幻灾难",
        "http://all.wasu.cn/index/sort/time/cid/7/class/program?&p=5" => "科幻灾难",
        "http://all.wasu.cn/index/sort/time/cid/7/class/program?&p=6" => "科幻灾难",
        
        "http://all.wasu.cn/index/sort/time/cid/4/class/program?&p=1" => "警匪动作",
        "http://all.wasu.cn/index/sort/time/cid/4/class/program?&p=2" => "警匪动作",
        "http://all.wasu.cn/index/sort/time/cid/4/class/program?&p=3" => "警匪动作",
        "http://all.wasu.cn/index/sort/time/cid/4/class/program?&p=4" => "警匪动作",
        "http://all.wasu.cn/index/sort/time/cid/4/class/program?&p=5" => "警匪动作",
        "http://all.wasu.cn/index/sort/time/cid/4/class/program?&p=6" => "警匪动作",
        
      }.each do |category_link, category_name|
        begin
          #打开电视剧分类页面
          puts "#{category_link} ... \n"
          category_page = open_link(category_link)
          category = Category.find_or_create_by_name_and_kind(category_name, 'movie')
          category_page.css('#publish dl').reverse.each do |dl|
            begin 
              image = dl.css('dt a img').first.attributes['original'].value
              movie_link = dl.css('dt a').first
              actors = dl.css('dd p')[2].css('a').map{|a|a.content}
              #打开电影页面
              video = Video.find_by_origin_url movie_link['href']
              if video
                video.update_attributes :category => category, :published => true
              else
                movie_page = open_link(movie_link['href'])
                info = movie_page.css('.play_filmcont p').last.at('#infoL').content.strip.split("：").last.to_s
                movie_name = movie_page.css('.movie_l h3').first.content
                video = Video.find_or_create_by_name movie_name
                video.generate_cover(image)
                link = movie_page.at('a.f_download')['onclick'].split("'")[3].strip

                video.update_attributes :link => link, :category => category, :origin_url => movie_link['href'],
                :kind => 'movie', :has_clip => false, :body => info
                actors.each do |actor_name|
                  actor = Actor.find_or_create_by_name actor_name
                  video.actors << actor
                end
                video.save
                puts "########  load video #{video.name} #{video.link}"
              end
            rescue #Timeout::Error
              puts "broken link when load video "
            end
          end
        rescue #Timeout::Error
          puts "broken link "
        end
      end
    end
    
    def open_link(link)
      puts "open link #{link}..."
      Nokogiri::HTML(open(link))
    end

    def load_page(link)
      puts "open #{SERVER+link} ..."
      Nokogiri::HTML(open("#{SERVER+link}"))
    end
  end
end
